package com.example.demo.controller;

import com.sun.xml.internal.bind.v2.model.core.EnumLeafInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


class Person{
    private int id;
    private String name;
    private double weight;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", weight=" + weight +
                '}';
    }
}

@RestController
public class ParamController {

    @RequestMapping("/m1")
    public String method1(String name){
        return "接收到的参数: " + name;
    }

    @RequestMapping("/m2")
    public String method2(String username, Integer age){
        return "接收到的参数name: " + username + ",age: " + age;
    }

    @RequestMapping("/m3")
    public String method3(Person p){
        return p.toString();
    }

    //前端参数key和后端key不同

    @RequestMapping("/m4")
    //RequestParam默认是true,此时前端发送的请求中必须带上这个key
    public String method4(@RequestParam(value = "username", required = false) String name){
        return "接收到的参数: " + name;
    }

    //传递数组
    @RequestMapping("/m5")
    public String method5(String[] strings){
        return Arrays.toString(strings);
    }

    //传递集合
    @RequestMapping("/m6")
    //如果参数是集合类的话则需要使用@RequestParam
    public String method6(@RequestParam List<String> listParam){
        return "size: " + listParam.size() + "," + listParam.toString();
    }

    //传递json格式数据
    @RequestMapping("/m7")
    //如果是json格式的数据,必须得加@RequestBody注解
    public String method7(@RequestBody Person person){
        return person.toString();
    }

    //获取URL中参数
    @RequestMapping("/m8/{id}/{username}/{weight}")
    //每一个参数都需要写一个@PathVariable
    public String method8(@PathVariable Integer id, @PathVariable("username") String name,  Double weight){
        return "路径上的参数为:id " + id + ",name " + name + ",weight " + weight;
    }

    //上传文件
    @RequestMapping("/m9")

    public String getfile(@RequestPart(value = "file", required = false) MultipartFile file) throws IOException {
       String fileName = file.getOriginalFilename();
       file.transferTo(new File("D:/temp" + file.getOriginalFilename()));
        return fileName;
    }

    @RequestMapping("/m10")
    public String getCookie1(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        StringBuilder stringBuilder = new StringBuilder();
        if(cookies == null){
            return stringBuilder.toString();
        }
        for(Cookie cookie : cookies){
           if(cookie.getName().equals("bbb")){
               stringBuilder.append(cookie.getValue());
           }
        }
        return stringBuilder.toString();

    }

    @RequestMapping("/m11")
    public String getCookie2(@CookieValue("ccc") String ccc, @CookieValue("bbb") String bbb){
        return "ccc的值为:" + ccc + " " + "bbb的值为:" + bbb;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("username", "zhangsan");
        return "ok";
    }

    //使用Servlet原生API
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session == null){
            return "null";
        }
        String ans = (String)session.getAttribute("username");
        return ans;
    }

    //使用注解来获取session对象
    //required = true 表示请求的时候必须要加上这个参数
    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute(value = "username", required = true) String name){
        return name;
    }

    //使用SpringMVC内置对象HttpSession
    @RequestMapping("/getSession3")
    public String getSession3(HttpSession session){
        String username = (String)session.getAttribute("username");
        return username;
    }

}
